#!/usr/bin/python

if False:
	print 'hello world'
	print "hello world";

if False:
	x=1;
	print x;
	print x==1;
	print x<1;

# while and if
if False:
	num = 10;
	running = True;
	while running:
		guess = int(raw_input('Enter an integer : '))
		if num <guess:
			print 'aa'
		elif num >guess:
			print 'bb'
		else:
			print 'cc';
			running = False;
	print 'you win'


# for loop
if False:
	for i in range(1,10):
		if i%2==0:
			print i;
		elif i ==5:
			continue;
		elif i > 8:
			break;
		else :
			print 'odd';

# define function
if False:
	def max(a,b):
		print 'hi';
		if a>b:
			return a;
		else:
			return b;

	# invoke fun
	foo1 = max(3,9);
	print foo1;

# default value
if False:
	def say(msg,time=1):
		print msg * time
	say('hi ',2);
	say('hi ')

# order and fun docs
if False:
	def gogo(a,b=5,c=10):
		'''this is the doc.'''
		print 'a=',a,'b=',b,'c=',c;
	gogo(1,c=9);
	gogo(c=3,a=1);
	print gogo.__doc__

# module
# ./script.py a b c
if False:
	import sys
	for i in sys.argv:
		print i;
	for i in sys.path:
		print i;

# use module
if False:
	import mymod
	mymod.sayHi();
	print mymod.version

# from ... import
if False:
	from mymod import sayHi,version
	sayHi();
	print 'version',version;


# dir() show property of module
if False:
	import sys
	dir(sys);

	a = 5;
	dir();# check output 
	del a; #delete a variable
	dir();# check output

# list
if False:
	shoplist = ['pear','apple','banana'];
	print len(shoplist);
	for item in shoplist:
		print item;
	shoplist.append('rice');

	print('-----------');
	shoplist.sort();
	for item in shoplist:
		print item;
	print('-----------');
	del shoplist[0];
	for item in shoplist:
		print item;

# immutable list
if False:
	zoo = ('wolf','elephant','penguin');
	print len(zoo);
	for item in zoo:
		print item;

	zoo2 = ('monkey','dolphin',zoo);
	print len(zoo2);
	for item in zoo2:
		print item;
	print zoo2[2][2];

# map
if False:
	ab = {
		'name':'wzq',
		'email':'mail'
	};
	print ab['name'];
	ab['age'] = 32;
	del ab['email'];
	print len(ab);
	for item in ab:
		print item,ab[item];

# lambda
if False:
	def repeater(n):
		return lambda s:s*n
	twice = repeater(2);
	cubic = repeater(3);
	print twice('word ');
	print cubic(4);


# try...except
if False:
	try:
		s = raw_input('Enter something:');
		n = int(s);
		print 'num',n;
	except:
		print 'cannot parse to number';

# try...finally ctrl+D trigger exp
if False:
	try:
		f= file('./script.py');
		running = True;
		while running:
			input = raw_input('press enter to continue:');
			line = f.readline();
			print line;
	except :
		print 'error happend:======='

	finally:
		print 'clean up'
		f.close();

# file io
if False:
	f=file('./script.py'); 
	# file(path, mode)
	# mode 'w','r','a' = write/read/append
	# default 'r'
	lineNum = 0;
	while True:
		line = f.readline();
		if len(line)==0: # eof
			break;
		lineNum +=1;
		print lineNum, line;
	f.close();

# storage
if True:
	import cPickle as p # cPicker much faster
	#import pickle as p
	shoplist = ['apple','mongo'];
	#write to file
	f = file('tmp.data','w');
	p.dump(shoplist,f);
	f.close();

	del shoplist;

	#reload from file
	f = file('tmp.data');
	list2 = p.load(f);
	print list2;